package com.core.app.window.view.template;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.dialogs.ConfirmDialog;

import com.core.app.event.CallbackEvent;
import com.core.app.window.view.data.DataPrestasiSekolahView;
import com.core.app.window.view.data.DataSekolahView;
import com.core.app.window.view.detail.DetailPrestasiSekolahView;
import com.core.context.Inject;
import com.core.entity.PrestasiSekolah;
import com.core.service.PrestasiSekolahService;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.Runo;

public class PrestasiTemplateView extends CustomComponent {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button deleteButton;
	@AutoGenerated
	private Button editButton;
	@AutoGenerated
	private Label deskripsiPrestasiLabel;
	@AutoGenerated
	private Label namaPrestasiLabel;
	private PrestasiSekolah prestasiSekolah;
	private DataPrestasiSekolahView dataPrestasiSekolahView;
	@Autowired
	private PrestasiSekolahService prestasiSekolahService;

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	public PrestasiTemplateView(final PrestasiSekolah prestasiSekolah,
			final DataPrestasiSekolahView dataPrestasiSekolahView) {
		Inject.Inject(this);
		buildMainLayout();
		setCompositionRoot(mainLayout);
		this.dataPrestasiSekolahView = dataPrestasiSekolahView;
		namaPrestasiLabel.addStyleName(Runo.LABEL_H2);
		setPrestasiSekolah(prestasiSekolah);
		// TODO add user code here

		editButton.addStyleName(Runo.BUTTON_SMALL);
		editButton.addStyleName(Runo.BUTTON_DEFAULT);
		editButton.addListener(new Listener() {

			private static final long serialVersionUID = 324806216758905106L;

			@Override
			public void componentEvent(Event event) {
				Window editWindow = new Window("Edit Prestasi");
				editWindow.setModal(true);
				DetailPrestasiSekolahView prestasiSekolahView = new DetailPrestasiSekolahView(
						getPrestasiSekolah(), new CallbackEvent() {

							@Override
							public void callback(Object object) {
								PrestasiSekolah prestasiSekolah = (PrestasiSekolah) object;
								PrestasiTemplateView.this
										.setPrestasiSekolah(prestasiSekolah);
							}
						});
				editWindow.setContent(prestasiSekolahView);
				getApplication().getMainWindow().addWindow(editWindow);
			}
		});

		deleteButton.addStyleName(Runo.BUTTON_SMALL);
		deleteButton.addStyleName(Runo.BUTTON_DEFAULT);
		deleteButton.addListener(new Listener() {

			private static final long serialVersionUID = 324806216758905106L;

			@Override
			public void componentEvent(Event event) {
				ConfirmDialog.show(PrestasiTemplateView.this.getApplication()
						.getMainWindow(), "Are you sure to delete?",
						new ConfirmDialog.Listener() {

							private static final long serialVersionUID = -5331318794164071160L;

							@Override
							public void onClose(ConfirmDialog dialog) {
								if (dialog.isConfirmed()) {
									if (dataPrestasiSekolahView
											.getPrestasiSekolahs().remove(
													prestasiSekolah)) {
										try {
											if (prestasiSekolah.getId() != null)
												prestasiSekolahService
														.delete(prestasiSekolah);
											dataPrestasiSekolahView.loadData();
										} catch (Exception e) {
											PrestasiTemplateView.this
													.getApplication()
													.getMainWindow()
													.showNotification(
															"Delete failed!",
															e.getMessage(),
															Notification.TYPE_ERROR_MESSAGE);
											e.printStackTrace();
										}
									}
								}
							}
						});
			}
		});
	}

	public PrestasiSekolah getPrestasiSekolah() {
		prestasiSekolah.setNama(namaPrestasiLabel.getValue().toString());
		prestasiSekolah.setDeskripsi(deskripsiPrestasiLabel.getValue()
				.toString());
		return prestasiSekolah;
	}

	public void setPrestasiSekolah(PrestasiSekolah prestasiSekolah) {
		this.prestasiSekolah = prestasiSekolah;
		if (prestasiSekolah != null) {
			namaPrestasiLabel.setValue(prestasiSekolah.getNama());
			deskripsiPrestasiLabel.setValue(prestasiSekolah.getDeskripsi());
		}
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);

		// top-level component properties
		setWidth("100.0%");
		setHeight("-1px");

		// namaPrestasiLabel
		namaPrestasiLabel = new Label();
		namaPrestasiLabel.setImmediate(false);
		namaPrestasiLabel.setWidth("-1px");
		namaPrestasiLabel.setHeight("-1px");
		namaPrestasiLabel.setValue("Nama");
		mainLayout.addComponent(namaPrestasiLabel);

		// deskripsiPrestasiLabel
		deskripsiPrestasiLabel = new Label();
		deskripsiPrestasiLabel.setImmediate(false);
		deskripsiPrestasiLabel.setWidth("100.0%");
		deskripsiPrestasiLabel.setHeight("-1px");
		deskripsiPrestasiLabel.setValue("Deskripsi");
		deskripsiPrestasiLabel.setContentMode(3);
		mainLayout.addComponent(deskripsiPrestasiLabel);

		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		mainLayout.addComponent(horizontalLayout_1);
		mainLayout.setComponentAlignment(horizontalLayout_1, new Alignment(34));

		return mainLayout;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("-1px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		horizontalLayout_1.setSpacing(true);

		// editButton
		editButton = new Button();
		editButton.setCaption("Edit");
		editButton.setImmediate(true);
		editButton.setWidth("-1px");
		editButton.setHeight("-1px");
		horizontalLayout_1.addComponent(editButton);

		// deleteButton
		deleteButton = new Button();
		deleteButton.setCaption("Delete");
		deleteButton.setImmediate(true);
		deleteButton.setWidth("-1px");
		deleteButton.setHeight("-1px");
		horizontalLayout_1.addComponent(deleteButton);

		return horizontalLayout_1;
	}

}
